package com.dsz.day4.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

public class runthread3callable {

  //jdk 5.0 提供了callable和Future接口
  public static void main(String[] args) throws Exception {
//      3.创建一个callable接口的实现类对象
      Callable<String> c1 = new MyCallable(10);
//      4.把callable对象的封装成一个真正的线程任务对象FutureTask对象
      FutureTask<String> r1 = new FutureTask<>(c1);//多态
//      5.把线程任务对象交给一个线程对象来处理
      Thread t1 = new Thread(r1);
      //6.启动线程
      t1.start();
      System.out.println("主线程运行中" + r1.get());
  }

}
//1.定义一个实现类来实现Callable接口
class MyCallable implements Callable<String> {
     private  int n;
     public MyCallable(int n){
         this.n=n;
     }
     //2.实现Callable接口中的call方法
     public String call() throws Exception{
         int sum=0;
         for(int i=0;i<=n;i++){
             sum+=i;
         }
         return "子线程的输出是="+sum;
     }
}